/*
  4位指令定义 以字串方式发送 0000 第一位收到指令返回1 第二位1为常开0为自动  第三位led对应编号1-2 第四位开关状态 1开0关

*/

#include "Arduino.h"

#define LED1 2 //定义led1脚位常量
#define LED2 3 //定义led2脚位常量

#define INPUT_BUTTON1 A4 //定义输入脚位
#define INPUT_BUTTON2 A5 //定义输入脚位

unsigned long nowtime1 = 0;
unsigned long nowtime2 = 0;

unsigned long nowtimeserial = 0;

bool check_button1 = false; //检测是否按下按钮
bool check_button2 = false; //检测是否按下按钮

bool run1 = true;//内部判断
bool run2 = true;//内部判断
int time1delay = 500 ; //延时500毫秒
int time2delay = 1000 ; //延时1秒

// 初始化
void setup() {
  // 串口输出 设置波特率
  Serial.begin(115200);

  // 按钮初始化
  pinMode(INPUT_BUTTON1, INPUT_PULLUP);
  pinMode(INPUT_BUTTON2, INPUT_PULLUP);

  //初始化led1-led3灯为输出模式以及低电平
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);

  digitalWrite(LED1, LOW);
  digitalWrite(LED2, LOW);
  delay(100);
  //串口打印输出信息
  //Serial.println("初始化完成.");
}

//串口读取函数
String incomedata; //串口接收到的数据
bool gotext = false;
void readdata() {
  while (Serial.available() > 0)//串口接收到数据
  {
    char inputdata = Serial.read();//获取串口接收到的数据
    incomedata += inputdata; //将接收到的字串合并
    //Serial.println(incomedata);

    if (incomedata.length() > 1) {
      gotext = true ; //判断是否接收到数据
    }
  }
}

//判断串口数据
void textcheck(String temp)
{
  temp.trim();
  //led1 自动模式自动开关
  if (temp == "1=on" or temp == "0011")
  {
    check_button1 = true;
    incomedata = "";//初始化串口接收数据，为下一次接收数据准备
  }
  //led1 关闭
  else if (temp == "1=off" or temp == "0010")
  {
    digitalWrite(LED1, LOW);
    Serial.println("1010");//确认返回信息
    check_button1 = false;
    nowtime1 -= time1delay; //时间超时
    incomedata = "";//初始化串口接收数据，为下一次接收数据准备
    run1 = true;
  }
  //led1 常开
  else if (temp == "1=alwayson" or temp == "0111")
  {
    digitalWrite(LED1, HIGH);
    Serial.println("1111");//确认返回信息
    check_button1 = false;
    nowtime1 -= time1delay; //时间超时
    incomedata = "";//初始化串口接收数据，为下一次接收数据准备
    run1 = true;
  }
  //led2 自动模式自动开关
  else if (temp == "2=on"  or temp == "0021")
  {
    check_button2 = true;
    incomedata = ""; //初始化串口接收数据，为下一次接收数据准备
  }
  //led2 关闭
  else if (temp == "2=off" or temp == "0020")
  {
    digitalWrite(LED2, LOW);
    Serial.println("1020");//确认返回信息
    check_button2 = false;
    nowtime2 -= time2delay; //时间超时
    incomedata = "";//初始化串口接收数据，为下一次接收数据准备
    run1 = true;
  }
  //led2 常开
  else if (temp == "2=alwayson" or temp == "0121")
  {
    digitalWrite(LED2, HIGH);
    Serial.println("1121"); //确认返回信息
    check_button2 = false;
    nowtime2 -= time2delay; //时间超时
    incomedata = ""; //初始化串口接收数据，为下一次接收数据准备
    run1 = true;
  }
  //指令不再范围错误返回
  else {
    Serial.println("!ERROR!");
    incomedata = "";//初始化串口接收数据，为下一次接收数据准备
  }
  gotext = false;
}

//循环程序开始
void loop() {

  //串口讯息读取 10ms 检测端口
  if (((unsigned long)millis() - nowtimeserial) > 10) {
    nowtimeserial = millis();
    readdata();
  }

  //判断串口输入的信息
  if (gotext) {
    textcheck(incomedata);
  }

  //按钮判断是否按下去
  if (digitalRead(INPUT_BUTTON1) == LOW) {
    check_button1 = true;

  }
  if (digitalRead(INPUT_BUTTON2) == LOW) {
    check_button2 = true;

  }



  if ( check_button1 == true) {
    //run1 = true;
    if (run1) {
      digitalWrite(LED1, HIGH);
      nowtime1 = millis();
      Serial.println("1011");//确认返回信息
      run1 = false;
    }
    //
    if ((unsigned long)(millis() - nowtime1) > time1delay) {
      nowtime1 = millis();
      digitalWrite(LED1, LOW);
      check_button1 = false;
      run1 = true;
    }
  }

  if ( check_button2 == true) {
    //run2 = true;
    if (run2) {
      digitalWrite(LED2, HIGH);
      nowtime2 = millis();
      Serial.println("1021");//确认返回信息
      run2 = false;
    }

    if ((unsigned long)(millis() - nowtime2) > time2delay) {
      nowtime2 = millis();
      digitalWrite(LED2, LOW);
      check_button2 = false;
      run2 = true;
    }

    //delay(10);
  }
}